package model

import (
	"auth/model/entity"
	"time"
)

var (
	TokenExpTime     = time.Hour * 24 * 7 // 测试过期时间
	RandStringNumber = 16
)

type UserType string

const (
	Admin       UserType = "001" // 后台管理用户
	App         UserType = "100" // 普通app车辆用户
	Store       UserType = "010" // 经销商用户
	AppAndStore UserType = "110" // 车辆用户和经销商用户
	AppAndAdmin UserType = "101" // 车辆用户和后台管理用户
)

type UserLoginRequest struct {
	Phone string `json:"phone" form:"phone" binding:"required,max=20"`

	//Code string `json:"code" form:"password" binding:"required,max=8"`

	Password string `json:"password" form:"password" binding:"required"`
}

type UserLoginResponse struct {
	Token string `json:"token"`

	Phone string `json:"phone"`

	Name string `json:"name"`

	Status int `json:"status"`

	Picture string `json:"picture"`

	RoleIds []int64 `json:"role_ids"`

	RoleNames []string `json:"role_names"`
}

type IsDisabledRequest struct {
	ID int64 `json:"id" binding:"required"`

	Status *int `json:"status" binding:"required"`
}
type ResetPasswordRequest struct {
	ID int64 `json:"id" binding:"required"`

	Password string `json:"password" binding:"required"`
}

type ChangePasswordRequest struct {
	ID int64 `json:"id" binding:"required"`

	OldPwd string `json:"old_pwd" binding:"required"`

	Password string `json:"password" binding:"required"`
}

type UserCreateRequest struct {
	Phone string `json:"phone" binding:"required"`

	Name string `json:"name" binding:"required"`

	WxID string `json:"wx_id"`

	AppleID string `json:"apple_id"`

	Password string `json:"password" binding:"required"`

	Status *int `json:"status" binding:"required"`

	Picture string `json:"picture"`

	RoleIds []int64 `json:"roleIds"`

	JobIds []int64 `json:"jobIds"`

	Type string `json:"type" binding:"required"`

	DeptId int64 `json:"dept_id"`

	AuthorityIds []int64 `json:"authorityIds"`
}
type FindSysByIdRequest struct {
	ID int64 `json:"id" binding:"required"`
}

type UserUpdateRequest struct {
	ID int64 `json:"id" binding:"required"`

	Phone *string `json:"phone"`

	Name *string `json:"name"`

	WxID *string `json:"wx_id"`

	AppleID *string `json:"apple_id"`

	Password *string `json:"password"`

	Status *int `json:"status"`

	RoleIds []int64 `json:"role_ids"`

	JobIds []int64 `json:"jobIds"`

	Picture *string `json:"picture"`

	CreatedAt *int64 `json:"created_at"`

	DeptId       int64   `json:"dept_id"`
	AuthorityIds []int64 `json:"authorityIds"`
}

type UserListRequest struct {
	Index int `json:"index" form:"index"`

	Size int `json:"size" form:"size"`

	UserId int64 `json:"user_id"`

	Phone string `json:"phone" form:"phone"`

	Name string `json:"name" form:"name"`

	Status *int `json:"status" form:"status"`

	StartTime int64 `json:"start_time"`

	EndTime int64 `json:"end_time"`
}
type GetSysUserListRequest struct {
	Index int `json:"index" form:"index"`

	Size int `json:"size" form:"size"`

	DeptId int64 `json:"dept_id"`
}

type SetUserAuthoritiesRequest struct {
	ID           int64   `json:"id" binding:"required"`
	AuthorityIds []int64 `json:"authorityIds" binding:"required"` // 角色ID
}

type UserListResponse struct {
	Total int64 `json:"total"`

	Data []*UsersInfoResponse `json:"list"`
}
type GetSysUserListResponse struct {
	Total int64 `json:"total"`

	Data []*GetSysUserList `json:"list"`
}

type UsersInfoResponse struct {
	ID int64 `json:"id"`

	Phone string `json:"phone"`

	Name string `json:"name"`

	Status int `json:"status"`

	RoleIds []int64 `json:"role_ids"`

	DeviceCount int `json:"device_count"` // 设备数量

	Picture string `json:"picture"`

	Type string `json:"type"`

	CreatedAt int64 `json:"created_at"`

	UpdatedAt int64 `json:"updated_at"`

	Authorities []*AuthorityInfo `json:"authorities"`

	Jobs []*JobInfo `json:"jobs"`

	DeptId int64 `json:"dept_id"`

	DeptName string `json:"dept_name"`
}
type UserInfoResponse struct {
	ID int64 `json:"id"`

	Phone string `json:"phone"`

	Name string `json:"name"`

	Status int `json:"status"`

	RoleIds []int64 `json:"role_ids"`

	Picture string `json:"picture"`

	Type []string `json:"type"`

	WxID string `json:"wx_id"`

	AppleID string `json:"apple_id"`

	CreatedAt int64 `json:"created_at"`
}

type FindSysByIdResponse struct {
	Authorities []*AuthorityInfo `json:"authorities"`
	Jobs        []*JobInfo       `json:"jobs"`
	UserInfo    *UserInfo        `json:"user_info"`
}

type UserInfo struct {
	ID int64 `json:"id"`

	Phone string `json:"phone"`

	Name string `json:"name"`

	WxID string `json:"wx_id"`

	AppleID string `json:"apple_id"`

	Password string `json:"password"`

	Salt string `json:"salt"`

	Status int `json:"status"`

	RoleIds []int64 `json:"role_ids"`

	Picture string `json:"picture"`

	Type string `json:"type"`

	CreatedAt int64 `json:"created_at"`

	UpdatedAt int64 `json:"updated_at"`

	DeptName string `json:"dept_name"`
}

type UserBaseRequest struct {
	ID int64 `json:"id" binding:"required"`
}

type UserDeleteRequest struct {
	ID int64 `json:"id" binding:"required"`
}

type GetSysUserList struct {
	ID          int64            `json:"id"`
	Phone       string           `json:"phone"`
	Name        string           `json:"name"`
	Status      int              `json:"status"`
	Picture     string           `json:"picture"`
	Type        string           `json:"type"`
	CreatedAt   int64            `json:"created_at"`
	UpdatedAt   int64            `json:"updated_at"`
	Authorities []*AuthorityInfo `json:"authorities" gorm:"-"`
	DeptId      int64            `json:"dept_id"`
	DeptName    string           `json:"dept_name" `
}

func UsersEntityToInfoResponse(users []*entity.User) []*UsersInfoResponse {
	out := make([]*UsersInfoResponse, 0, len(users))
	for _, c := range users {
		out = append(out, UserEntityToInfoResponse(c))
	}
	return out
}

func UserEntityToInfoResponse(e *entity.User) *UsersInfoResponse {
	return &UsersInfoResponse{

		ID: e.ID,

		Phone: e.Phone,

		Name: e.Name,

		Status: e.Status,

		RoleIds: e.RoleIds,

		Picture: e.Picture,

		Type: e.Type,

		DeptId: e.DeptId,

		CreatedAt: e.CreatedAt,

		UpdatedAt: e.UpdatedAt,
	}
}

func UsersEntityToDto(users []*entity.User) []*UserInfo {
	out := make([]*UserInfo, 0, len(users))
	for _, c := range users {
		out = append(out, UserEntityToDto(c))
	}
	return out
}

type UserCount struct {
	Count int64 `json:"count"` // 用户数量

	HaveDeviceCount int64 `json:"have_device_count"` // 有车用户数量

	ShareDeviceCount int64 `json:"share_device_count"` // 分享用车用户数量

}

var UserTypes = []string{string(App), string(AppAndAdmin), string(AppAndStore)}
var SysUserTypes = []string{string(Admin), string(Store), string(AppAndAdmin), string(AppAndStore)} //系统管理用户

func UserEntityToDto(e *entity.User) *UserInfo {
	return &UserInfo{

		ID: e.ID,

		Phone: e.Phone,

		Name: e.Name,

		WxID: e.WxID,

		AppleID: e.AppleID,

		Password: e.Password,

		Salt: e.Salt,

		Status: e.Status,

		RoleIds: e.RoleIds,

		Picture: e.Picture,

		Type: e.Type,

		CreatedAt: e.CreatedAt,

		UpdatedAt: e.UpdatedAt,
	}
}

/******************************************************* APP **********************************************/

type LoginType int

const (
	AppLoginPassword  LoginType = 0
	AppLoginCode      LoginType = 1
	AppLoginWX        LoginType = 2
	AppLoginApple     LoginType = 3
	AppLoginPurePhone LoginType = 4
)

type UserAppLoginRequest struct {
	Phone string `json:"phone"`

	Type *int `json:"type" form:"type" binding:"required"`

	WxID string `json:"wx_id"`

	AppleID string `json:"apple_id"`

	Password string `json:"password"`

	Code *int `json:"code"`
}

type UserAppBaseRequest struct {
	ID int64 `json:"id" binding:"required"`
}

type UserAppLoginResponse struct {
	Token string `json:"token"`

	Phone string `json:"phone"`

	Name        string `json:"name"`
	AppleID     string `json:"apple_id"`
	WxID        string `json:"wx_id"`
	PasswordSet bool   `json:"password_set"` //是否设置过密码

	Status int `json:"status"`

	Picture string `json:"picture"`

	RoleIds []int64 `json:"role_ids"`

	RoleNames []string `json:"role_names"`

	Type string `json:"type"`
}

type UserAppInfoResponse struct {
	ID int64 `json:"id"`

	Phone string `json:"phone"`

	Name string `json:"name"`

	Status int `json:"status"`

	RoleIds []int64 `json:"role_ids"`

	Picture string `json:"picture"`

	Type string `json:"type"`

	WxID string `json:"wx_id"`

	AppleID string `json:"apple_id"`

	CreatedAt int64 `json:"created_at"`
}

// AppUserPhoneReplaceRequest 用户修改手机号
type AppUserPhoneReplaceRequest struct {
	Phone *string `json:"phone" binding:"required"`
	Code  *int    `json:"code" binding:"required"`
}

// AppUserSetPasswordRequest 用户设置登陆密码
type AppUserSetPasswordRequest struct {
	Code     *int    `json:"code" binding:"required"`
	Password *string `json:"password" binding:"required"`
}

// AppUserUpdatePasswordRequest 用户修改密码
type AppUserUpdatePasswordRequest struct {
	PasswordOld *string `json:"password_old" binding:"required"`
	PasswordNew *string `json:"password_new" binding:"required"`
}

// AppUserUpdateRequest 用户信息修改
type AppUserUpdateRequest struct {
	Name    *string `json:"name"`
	Picture *string `json:"picture"`
}

// AppForgotPasswordRequest 忘记密码
type AppForgotPasswordRequest struct {
	Phone    *string `json:"phone" binding:"required"`
	Code     *int    `json:"code" binding:"required"`
	Password *string `json:"password" binding:"required"`
}

type ThirdPartyIDBindRequest struct {
	Phone             string `json:"phone" binding:"required"`
	ThirdPartyAccount string `json:"third_party_account" binding:"required"`
	Code              *int   `json:"code" binding:"required"`
}
